package cn.ffcs.uec.selfstaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecDocEduEditBean;
import cn.ffcs.uec.staff.model.UecDocEdu;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecSelfDocEduEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecDocEduEditBean bean = new UecDocEduEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecDocEdu uecDocEdu;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecDocEduEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		this.bindComboboxBean();
	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecDocEduEditWindow().setTitle("新增教育经历信息");
			bean.getUecDocEduEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecDocEduEditWindow().setTitle("查看教育经历信息");
			uecDocEdu = (UecDocEdu) arg.get("uecDocEdu");
			if (null != uecDocEdu) {
				PubUtil.fillBeanFromPo(uecDocEdu, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecDocEduEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecDocEduEditWindow().setTitle("编辑教育经历信息");
			bean.getUecDocEduEditDiv().setHeight("30%");
			uecDocEdu = (UecDocEdu) arg.get("uecDocEdu");
			if(null != uecDocEdu){
				PubUtil.fillBeanFromPo(uecDocEdu, bean);
				stringTransToDate();
			}
			
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecDocEdu = UecDocEdu.newInstance();
			PubUtil.fillPoFromBean(bean, uecDocEdu);
			dateTransToString();
			uecDocEdu.setStaffId(uecStaffInfo.getStaffId());
			uecDocEdu.addOnly();
			Events.postEvent("onOK", this.self, uecDocEdu);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecDocEdu);
			dateTransToString();
			uecDocEdu.updateOnly();
			Events.postEvent("onOK", this.self, uecDocEdu);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecDocEduEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// }
		return null;
	}
	/**
	 * 将Date类型时间转成String类型
	 * Date --> String
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getDegreedate().getValue())) {
			// 学位授予日期
			uecDocEdu.setDegreedate(formatter.format(bean.getDegreedate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef3().getValue())) {
			// 取得学历时间
			uecDocEdu
					.setGlbdef3(formatter.format(bean.getGlbdef3().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			// 入学日期
			uecDocEdu.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			// 毕业日期
			uecDocEdu
					.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}
	}
	
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 学位授予日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecDocEdu.getDegreedate()) {
			bean.getDegreedate()
					.setValue(sdf.parse(uecDocEdu.getDegreedate()));
		}
		// 取得学历时间
		if (null != uecDocEdu.getGlbdef3()) {
			bean.getGlbdef3()
					.setValue(sdf.parse(uecDocEdu.getGlbdef3()));
		}
		// 入学日期
		if (null != uecDocEdu.getBegindate()) {
			bean.getBegindate()
					.setValue(sdf.parse(uecDocEdu.getBegindate()));
		}
		// 毕业日期
		if (null != uecDocEdu.getEnddate()) {
			bean.getEnddate()
					.setValue(sdf.parse(uecDocEdu.getEnddate()));
		}
		
	}
	
	/**
	 * 未采用数据字典，这里是暂时的转换,将枚举量翻译
	 */
	public void bindComboboxBean() {
		// 学校类型
		List<NodeVo> nodeVoSchoolTypeList = new ArrayList<NodeVo>();
		String[][] schoolType = {{"1001A1100000000006BJ", "重点综合类高校"},
				{"1001A1100000000006BK", "清华大学"},
				{"1001A1100000000006BL", "北京大学"},
				{"1001A1100000000006BM", "中国人民大学"},
				{"1001A1100000000006BN", "上海交通大学"},
				{"1001A1100000000006BO", "复旦大学"},
				{"1001A1100000000006BP", "同济大学"},
				{"1001A1100000000006BQ", "浙江大学"},
				{"1001A1100000000006BR", "中山大学"},
				{"1001A1100000000006BS", "西安交通大学"},
				{"1001A1100000000006BT", "复旦大学"},
				{"1001A1100000000006BO", "武汉大学"},
				{"1001A1100000000006BU", "四川大学"},
				{"1001A1100000000006BV", "邮电高校"},
				{"1001A1100000000006BW", "北京邮电大学"},
				{"1001A1100000000006BX", "南京邮电大学"},
				{"1001A1100000000006BY", "重庆邮电大学"},
				{"1001A1100000000006BZ", "西安邮电大学"},
				{"1001A1100000000006C0", "其他211院校"},
				{"1001A1100000000006C1", "其他普通高校"},
				{"1001A1100000000006C2", "其他"}};
		for (int i = 0; i < schoolType.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(schoolType[i][j]);
				nodeVo.setName(schoolType[i][j + 1]);
			}
			nodeVoSchoolTypeList.add(nodeVo);
		}
		ListboxUtils
				.rendererForEdit(bean.getSchooltype(), nodeVoSchoolTypeList);

		// 学习方式studymode
		List<NodeVo> nodeVoStudymodeList = new ArrayList<NodeVo>();
		String[][] studymode = {{"1001A1100000000006B4", "全日制"},
				{"1001A1100000000006B5", "统招"}, {"1001A1100000000006B6", "自费"},
				{"1001A1100000000006B7", "内招"}, {"1001A1100000000006B8", "委培"},
				{"1001A1100000000006B9", "非全日制"},
				{"1001A1100000000006BA", "电大"}, {"1001A1100000000006BB", "夜大"},
				{"1001A1100000000006BC", "函授"}, {"1001A1100000000006BD", "党校"},
				{"1001A1100000000006BE", "自考"}, {"1001A1100000000006BF", "业余"},
				{"1001A1100000000006BG", "网络教育"},
				{"1001A1100000000006BH", "在职研究生"},
				{"1001A1100000000006BI", "其他"}};
		for (int i = 0; i < studymode.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(studymode[i][j]);
				nodeVo.setName(studymode[i][j + 1]);
			}
			nodeVoStudymodeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getStudymode(), nodeVoStudymodeList);

		// 学历education
		List<NodeVo> nodeVoEduList = new ArrayList<NodeVo>();
		String[][] edu = {{"1001A1100000000006AN", "硕士研究生"},
				{"1001A1100000000006AO", "研究生同等学历"},
				{"1001A1100000000006AP", "大学本科"},
				{"1001A1100000000006AQ", "专科"}, {"1001A1100000000006AR", "中专"},
				{"1001A1100000000006AS", "高中"},
				{"1001A1100000000006AT", "技工学校"},
				{"1001A1100000000006AU", "初中"}, {"1001A1100000000006AV", "小学"},
				{"1001A1100000000006AW", "无"},
				{"1001A1100000000006AX", "大学普通班"}};
		for (int i = 0; i < edu.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(edu[i][j]);
				nodeVo.setName(edu[i][j + 1]);
			}
			nodeVoEduList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getEducation(), nodeVoEduList);

		// 学位pkDegree
		List<NodeVo> nodeVoPkDegreeList = new ArrayList<NodeVo>();
		String[][] pkDegree = {{"1001A1100000000006AY", "博士"},
				{"1001A1100000000006AZ", "双硕士"},
				{"1001A1100000000006B0", "硕士"},
				{"1001A1100000000006B1", "双学士"},
				{"1001A1100000000006B2", "学士"}, {"1001A1100000000006B3", "无"}};
		for (int i = 0; i < pkDegree.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(pkDegree[i][j]);
				nodeVo.setName(pkDegree[i][j + 1]);
			}
			nodeVoPkDegreeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPkDegree(), nodeVoPkDegreeList);

		// 是否第一学历
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = {{"Y", "是"}, {"N", "否"}};
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef5(), nodeVoIshiskeypsnList);
		// 是否最高学历
		ListboxUtils.rendererForEdit(bean.getLasteducation(),
				nodeVoIshiskeypsnList);
		// 是否最高学位
		ListboxUtils.rendererForEdit(bean.getGlbdef4(), nodeVoIshiskeypsnList);
		// 是否全日制最高学历
		ListboxUtils.rendererForEdit(bean.getGlbdef8(), nodeVoIshiskeypsnList);
		// 是否在职最高学历
		ListboxUtils.rendererForEdit(bean.getGlbdef9(), nodeVoIshiskeypsnList);
		
		//学历专业类别majortype
		List<NodeVo> nodeVoMajortypeList = new ArrayList<NodeVo>();
		String[][] majortype = {
				{"1001ZZ100000006TL09J","哲学"},
				{"1001ZZ100000006TL09K","经济学"},
				{"1001ZZ100000006TL09L","法学"},
				{"1001ZZ100000006TL09M","教育学"},
				{"1001ZZ100000006TL09N","文学"},
				{"1001ZZ100000006TL09O","历史学"},
				{"1001ZZ100000006TL09P","理学"},
				{"1001ZZ100000006TL09Q","工学"},
				{"1001ZZ100000006TL09R","农学"},
				{"1001ZZ100000006TL09S","医学"},
				{"1001ZZ100000006TL09T","管理学"},
				{"1001ZZ100000006TL09U","艺术学"},
				{"1001A1100000000006OK","通信类"},
				{"1001A1100000000006OL","计算机类"},
				{"1001A1100000000006OM","市场营销类"},
				{"1001A1100000000006ON","经济管理类"},
				{"1001A1100000000006OO","财会类"},
				{"1001A1100000000006OP","语言类"},
				{"1001A1100000000006OQ","其他"}
				};
		for (int i = 0; i < majortype.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(majortype[i][j]);
				nodeVo.setName(majortype[i][j + 1]);
			}
			nodeVoMajortypeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getMajortype(), nodeVoMajortypeList);
		
	}

}
